import random
import tkinter as tk
from tkinter import messagebox

def load_students(filename):
    """读取学生文件并返回非空行列表"""
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            return [line.strip() for line in f if line.strip()]
    except FileNotFoundError:
        return None

def main():
    # 初始化Tkinter并隐藏主窗口
    root = tk.Tk()
    root.withdraw()
    
    # 加载学生数据
    students = load_students('students.txt')
    
    if students is None:
        messagebox.showerror("错误", "找不到students.txt文件")
    elif not students:
        messagebox.showerror("错误", "文件中没有有效学生数据")
    else:
        selected_student = random.choice(students)
        messagebox.showinfo("点名结果", f"被选中的学生是：{selected_student}")
    
    # 关闭Tkinter
    root.destroy()

if __name__ == "__main__":
    main()